home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / util / typetest.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  128 lines

  1. /*    $Header: /usr/people/sam/fax/util/RCS/typetest.c++,v 1.1 1994/03/18 02:46:05 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1994 Sam Leffler
  4.  * Copyright (c) 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25.  
  26. /*
  27.  * Program for testing out typerules.
  28.  *
  29.  * Usage: typetest [-f rulesfile] files
  30.  */
  31. #include <stdlib.h>
  32. #include <stdarg.h>
  33.  
  34. #include <osfcn.h>
  35. #include <unistd.h>
  36. #include <sys/stat.h>
  37. #include <fcntl.h>
  38. #include <string.h>
  39.  
  40. #include "TypeRules.h"
  41. #include "config.h"
  42.  
  43. TypeRules* typeRules;
  44.  
  45. /*
  46.  * Return a TypeRule for the specified file.
  47.  */
  48. const TypeRule*
  49. fileType(const char* filename)
  50. {
  51.     struct stat sb;
  52.     int fd = ::open(filename, O_RDONLY);
  53.     if (fd < 0) {
  54.     fprintf(stderr, "%s: Can not open file\n", filename);
  55.     return (NULL);
  56.     }
  57.     if (fstat(fd, &sb) < 0) {
  58.     fprintf(stderr, "%s: Can not stat file\n", filename);
  59.     ::close(fd);
  60.     return (NULL);
  61.     }
  62.     if ((sb.st_mode & S_IFMT) != S_IFREG) {
  63.     fprintf(stderr, "%s: Not a regular file\n", filename);
  64.     ::close(fd);
  65.     return (NULL);
  66.     }
  67.     char buf[512];
  68.     int cc = read(fd, buf, sizeof (buf));
  69.     ::close(fd);
  70.     if (cc == 0) {
  71.     fprintf(stderr, "%s: Empty file\n", filename);
  72.     return (NULL);
  73.     }
  74.     const TypeRule* tr = typeRules->match(buf, cc);
  75.     if (!tr) {
  76.     fprintf(stderr, "%s: Can not determine file type\n", filename);
  77.     return (NULL);
  78.     }
  79.     if (tr->getResult() == TypeRule::ERROR) {
  80.     fxStr emsg(tr->getErrMsg());
  81.     fprintf(stderr, "%s: %s\n", filename, (char*) emsg);
  82.     return (NULL);
  83.     }
  84.     return tr;   
  85. }
  86.  
  87. const    fxStr TypeRulesFile(FAX_TYPERULES);
  88. char*    appName;
  89.  
  90. void
  91. usage()
  92. {
  93.     fprintf(stderr, "usage: %s [-f rulesfile] files\n", appName);
  94.     exit(-1);
  95. }
  96.  
  97. int
  98. main(int argc, char* argv[])
  99. {
  100.     extern int optind, opterr;
  101.     extern char* optarg;
  102.     int c;
  103.     fxStr file;
  104.  
  105.     appName = argv[0];
  106.     file = fxStr(FAX_LIBDATA) | "/" | TypeRulesFile;
  107.     while ((c = getopt(argc, argv, "f:")) != -1)
  108.     switch (c) {
  109.     case 'f':
  110.         file = optarg;
  111.         break;
  112.     case '?':
  113.         usage();
  114.         /*NOTREACHED*/
  115.     }
  116.     if (argc - optind < 1)
  117.     usage();
  118.     typeRules = TypeRules::read(file);
  119.     if (!typeRules) {
  120.     fprintf(stderr, "Unable to setup file typing and conversion rules\n");
  121.     return (-1);
  122.     }
  123.     typeRules->setVerbose(TRUE);
  124.     for (; optind < argc; optind++)
  125.     (void) fileType(argv[optind]);
  126.     return (0);
  127. }
  128.